#include "def.h"

int main() {
    init();
    eventLoop();
    return 0;
}



void init() {
    wind.create(VideoMode::getDesktopMode(),"MyWindow",Style::None,
                ContextSettings(24,0,0,4,5,0,false));
    wind.setActive(true);
    wind.setFramerateLimit(120);
    bgm.openFromFile("bgm/bgm.ogg");
    bgm.setVolume(50);
    bgm.setLoop(true);
    bgm.play();
    _w = VideoMode::getDesktopMode().width;
    _h = VideoMode::getDesktopMode().height;

    glewExperimental = true;
    glewInit();

    glEnable(GL_DEPTH_TEST);

    //加载这色器
    myShader.loadyShader("shader/shader.vs","shader/shader.fs");

    //VAO,VBO
    glGenBuffers(1,&VBO);
    glBindBuffer(GL_ARRAY_BUFFER,VBO);
    glGenVertexArrays(1,&VAO);
    glBindVertexArray(VAO);
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*) 0);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1,2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*) (3*sizeof (GLfloat)));
    glEnableVertexAttribArray(1);

    //纹理
    int w,h,n;
    unsigned char *data = stbi_load("img/t.jpeg",&w,&h,&n,0);
    glGenTextures(1,&img);
    glBindTexture(GL_TEXTURE_2D,img);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,w,h,0,GL_RGB,GL_UNSIGNED_BYTE,data);
    glGenerateMipmap(img);
    stbi_image_free(data);

    glClearColor(0.3,0.4,0.5,1.0);
}

void draw(Clock clock){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    myShader.use();

    projection = glm::perspective(glm::radians(70.0f), (float)_w / (float)_h, 0.1f, 100.0f);
    view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
    myShader.setMat4("view",view);
    myShader.setMat4("projection",projection);

    for (int i = 0; i < 10; i++) {
        glm::mat4 model = glm::mat4(1.0f);
        model = glm::translate(model, cubePositions[i]);
        myShader.setMat4("model", model);

        glDrawArrays(GL_TRIANGLES, 0, 36);
    }
}

void eventLoop() {
    Event e;
    Clock clock;
    while (wind.isOpen()){
        deltaTime = clock.restart().asSeconds();

        while (wind.pollEvent(e)){
            if(e.type == Event::Closed || e.key.code == Keyboard::Escape){
                wind.close();
            }

            if(e.type == Event::KeyPressed){
                float cameraSpeed = 2.5 * deltaTime;
                if (e.key.code == Keyboard::W)
                    cameraPos += cameraSpeed * cameraFront;
                if (e.key.code == Keyboard::S)
                    cameraPos -= cameraSpeed * cameraFront;
                if (e.key.code == Keyboard::A)
                    cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
                if (e.key.code == Keyboard::D)
                    cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
            }// endl KeyPressed
        }
        draw(clock);
        wind.display();
    }
}
